const express = require("express");
const LaborTeamModel = require("../models/laborTeamModel");
const router = express.Router();

/**
 * @swagger
 * tags:
 *   name: 劳务队伍管理
 *   description: 管理劳务队伍信息的接口
 */

/**
 * @swagger
 * /api/laborTeam:
 *   post:
 *     summary: 创建新的劳务队伍
 *     tags: [劳务队伍管理]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               leader:
 *                 type: integer
 *     responses:
 *       200:
 *         description: 创建成功
 *       500:
 *         description: 服务器错误
 */
router.post("/", async (req, res) => {
    try {
        const { name, leader } = req.body;
        const team = await LaborTeamModel.create(name, leader);
        res.json(team);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

/**
 * @swagger
 * /api/laborTeam:
 *   get:
 *     summary: 获取所有劳务队伍（排除已软删除）
 *     tags: [劳务队伍管理]
 *     responses:
 *       200:
 *         description: 成功返回队伍列表
 *       500:
 *         description: 服务器错误
 */
router.get("/", async (req, res) => {
    try {
        const teams = await LaborTeamModel.findAll();
        res.json(teams);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

/**
 * @swagger
 * /api/laborTeam/{id}:
 *   get:
 *     summary: 根据 ID 获取单个劳务队伍
 *     tags: [劳务队伍管理]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 成功返回队伍对象
 *       404:
 *         description: 未找到
 *       500:
 *         description: 服务器错误
 */
router.get("/:id", async (req, res) => {
    try {
        const team = await LaborTeamModel.findById(req.params.id);
        if (!team) return res.status(404).json({ error: "Not found" });
        res.json(team);
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

/**
 * @swagger
 * /api/laborTeam/{id}:
 *   put:
 *     summary: 更新指定 ID 的劳务队伍信息
 *     tags: [劳务队伍管理]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               leader:
 *                 type: integer
 *               status:
 *                 type: integer
 *                 description: 状态（1=正常，0=禁用）
 *     responses:
 *       200:
 *         description: 更新成功
 *       404:
 *         description: 未找到
 *       500:
 *         description: 服务器错误
 */
router.put("/:id", async (req, res) => {
    try {
        const { name, leader, status } = req.body;
        const success = await LaborTeamModel.update(req.params.id, name, leader, status);
        if (!success) return res.status(404).json({ error: "Not found" });
        res.json({ message: "Updated successfully" });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

/**
 * @swagger
 * /api/laborTeam/{id}:
 *   delete:
 *     summary: 软删除指定 ID 的劳务队伍
 *     tags: [劳务队伍管理]
 *     parameters:
 *       - in: path
 *         name: id
 *         required: true
 *         schema:
 *           type: integer
 *     responses:
 *       200:
 *         description: 删除成功（软删除）
 *       404:
 *         description: 未找到
 *       500:
 *         description: 服务器错误
 */
router.delete("/:id", async (req, res) => {
    try {
        const success = await LaborTeamModel.delete(req.params.id);
        if (!success) return res.status(404).json({ error: "Not found" });
        res.json({ message: "Deleted successfully (soft delete)" });
    } catch (err) {
        res.status(500).json({ error: err.message });
    }
});

module.exports = router;
